import { StoreOptions } from 'vuex'
import { IFileViewModel } from './ViewModels/FilesViewModel'
import http from '@/http';

interface ISearchFilesState {
    files: IFileViewModel[]
    typeCode: string
}

export const moduleSearchFiles: StoreOptions<ISearchFilesState> = {
    state: {
        files: [] as IFileViewModel[],
        typeCode: '',
    },
    mutations: {
        searchFilesRefresh(state, playload: { files: IFileViewModel[] }) {
            state.files = playload.files
        },
        searchScanCode(state, playload: { typeCode: string }) {
            state.typeCode = playload.typeCode
        }
    },
    actions: {
        async searchAsync({ commit }, playload: { search: string }) {
            let response = await http.get(`/api/Files/?search=${playload.search}`)
            commit('searchFilesRefresh', <{ files: IFileViewModel[] }>{ files: response.data })
        },
        async searchScanCodeAsync({ commit }, playload: { code: string }) {
            let response = await http.get(`/api/ScanCode?url=${playload.code}`);
            commit('searchScanCode', {typeCode: response.data})
        }   
    }
}